package com.software.seakdream.controller;

import com.software.seakdream.entity.UserInfo;
import com.software.seakdream.repository.ExchangeRepository;
import com.software.seakdream.repository.UserInfoRepository;
import com.software.seakdream.service.ExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/Exchange")
public class ExchangeController {
    @Autowired
    private ExchangeService exchangeService;
    @Autowired
    private UserInfoRepository userInfoRepository;

    @GetMapping("/getUserBonus")
    public int getUserBonusById(String userId) {
        return userInfoRepository.getPointsById(userId);
    }

    @GetMapping("/minusUserBonus/{userId}/{exchangeId}")
    public int minusUserBonus(@PathVariable(value = "userId") String userId, @PathVariable(value = "exchangeId") Integer exchangeId) {
        Integer userPoints = userInfoRepository.getPointsById(userId);
        Integer bonus = exchangeService.findBonusByExchangeId(exchangeId);
        userInfoRepository.updatePoints(userId, userPoints - bonus);
        return userPoints - bonus;
    }
}
